﻿using GenericHtml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WLFramework.Data;

namespace SampleSite.Common
{
    public class DynamicMetadataProvider : IDynamicMetadataProvider
    {
        public DynamicTable GetTable(string name)
        {
            using (CommandHelper db = new CommandHelper(CommandMethod.Read))
            {
                db.Context.BeginTransaction();
                var table = GetTable(db.Context, name);
                db.Context.CommitTransaction();

                return table;
            }
        }

        public static DynamicTable GetTable(PermissionContext context, string name)
        {
            var table = (from p in context.dy_DynamicTables
                         where p.TableName == name
                         select new DynamicTable
                         {
                             Id = p.Id,
                             Name = p.TableName,
                             Template = p.Template,
                             DisplayName = p.Description,
                         }).First();

            table.Columns = (from p in context.dy_DynamicColumns
                             where p.TableId == table.Id
                             select new DynamicColumn
                             {
                                 Id = p.Id,
                                 Name = p.ColumnName,
                                 DisplayName = p.DisplayName,
                                 ControlId = p.ControlId,
                                 TypeId = (ColumnType)p.TypeId,
                                 Required = p.Required,
                                 Precision = p.Precision,
                                 MinLength = p.MinLength,
                                 MaxLength = p.MaxLength,
                                 IsSystemDefault = p.IsSystemDefault,
                                 IsPrimaryKey = p.IsPrimaryKey,
                                 Edit = p.CanEdit
                             }).ToList();

            return table;
        }

        public List<DynamicTable> GetAllTables()
        {
            using (CommandHelper db = new CommandHelper(CommandMethod.Read))
            {
                db.Context.BeginTransaction();

                var tables = (from p in db.Context.dy_DynamicTables
                              where p.State == 1
                              select new DynamicTable
                              {
                                  Id = p.Id,
                                  Name = p.TableName,
                                  Template = p.Template,
                                  DisplayName = p.Description,
                              }).ToList();

                foreach (var t in tables)
                {
                    t.Columns = (from p in db.Context.dy_DynamicColumns
                                 where p.TableId == t.Id
                                 select new DynamicColumn
                                 {
                                     Id = p.Id,
                                     Name = p.ColumnName,
                                     DisplayName = p.DisplayName,
                                     ControlId = p.ControlId,
                                     TypeId = (ColumnType)p.TypeId,
                                     Required = p.Required,
                                     Precision = p.Precision,
                                     MinLength = p.MinLength,
                                     MaxLength = p.MaxLength,
                                     IsSystemDefault = p.IsSystemDefault,
                                     IsPrimaryKey = p.IsPrimaryKey,
                                     Edit = p.CanEdit
                                 }).ToList();
                }

                db.Context.CommitTransaction();

                return tables;
            }
        }

        public List<DynamicControl> GetControls()
        {
            using (CommandHelper db = new CommandHelper(CommandMethod.Read))
            {
                return (from p in db.Context.dy_ControlTypes
                        select new DynamicControl
                        {
                            Id = p.Id,
                            Name = p.TypeName,
                            HtmlTemplate = p.EditHtml,
                            Content = p.Content
                        }).ToList();
            }
        }
    }
}